package com.codehacker.job.mallservice.controller;

import com.codehacker.job.mallservice.dao.ClassifyMapper;
import com.codehacker.job.mallservice.dto.ResponseDTO;
import com.codehacker.job.mallservice.entity.Classify;
import com.codehacker.job.mallservice.entity.ClassifyExample;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

/**
 * ClassifyController
 *
 * @author zhuyuwei
 * @date 26/07/2017
 */
@Controller
@RequestMapping("/api/classify")
public class ClassifyController {
    @Resource
    private ClassifyMapper classifyMapper;

    @GetMapping("")
    @ResponseBody
    public ResponseDTO<Classify> pageSearch(@RequestParam("page") Integer page, @RequestParam("limit") Integer limit) {
        ResponseDTO<Classify> result = new ResponseDTO<Classify>();
        ClassifyExample example = new ClassifyExample();

        result.getData().getPaginate().setTotal((int) classifyMapper.countByExample(example));
        result.getData().getItems().addAll(classifyMapper.selectByExampleWithPaging(example, (page - 1) * limit, limit));
        result.getData().getPaginate().setNext(page + 1);
        if (CollectionUtils.isEmpty(result.getData().getItems())) {
            result.getData().getPaginate().setHasNext(false);
        } else {
            result.getData().getPaginate().setHasNext(result.getData().getPaginate().getTotal() < (page + 1) * limit ? false : true);
        }
        result.getData().getPaginate().setPerPage(limit);
        result.getMeta().setCode(0);
        return result;
    }
}
